<?php if (!defined('BASEPATH')) exit('No direct access allowed.');

/**
 * 用户注册服务
 */
class User_register_service extends MY_Service {

    public function __construct() {
        parent::__construct();
        $this->load->config('auth', true); //加载配置
        $this->load->service('user/validator/user_validator'); //用户验证类
        log_message('debug', 'User_register_service Service Class Initialized');
    }


    /**
     * 开始注册之前
     *
     * @return array 用户信息
     */
    public function before_register() {

    }

    /**
     * 用户注册
     *
     * @param string $username 用户名称
     * @param string $password 用户密码
     * @param string $email 用户邮箱
     * @return int $uid
     */
    public function register($username, $password, $email) {

    }

    /**
     * 完成注册后执行
     *
     * @param int $uid
     * @return array 用户信息
     */
    public function after_register($uid) {

    }


    /**
     * 检查默认用户组是否已经设置
     * 注册页面调用
     */
    public function check_default_group() {
        //注册激活用户默认用户组
        $activation_gid = $this->_config['default_group_activation'];
        //注册未激活用户默认用户组
        $unactivation_gid = $this->_config['default_group_unactivation'];
        //如果默认用户组没有设置
        if ((!$activation_gid or !$unactivation_gid)) {
            return $this->error->set_error('AUTH:register_not_set_default_usergroup');
        }
        return true;
    }

    /**
     * 检测是否规定时间内同一IP将无法进行多次注册
     * 注册页面调用
     *
     * @return true|Error
     */
    public function check_ip() {
        $hour = $this->_config['register_limit_hour'];
        if ($hour && $hour > 0) {
            $row = $this->get_register_security();
            if ($row && TIMESTAMP - $row['last_time'] < $hour * 3600) {
                return $this->error->set_error('AUTH:register_cannot_repeat', array('{hour}' => $hour));
            }
        }
        return true;
    }

    /**
     * 验证用户名
     *
     * @param string $username
     * @return boolean
     */
    public function check_username($username) {
        //用户名是否为空
        if (!$this->check_username_empty($username)) {
            return $this->error->set_error('AUTH:username_empty');
        }
        //用户名长度是否错误
        if (!$this->check_username_length($username)) {
            $min = $this->_config['min_username_length'];
            $max = $this->_config['max_username_length'];
            return $this->error->set_error('AUTH:username_length_error', array('min' => $min, 'max' => $max));
        }
        //检查用户名格式
        if (!$this->check_username_format($username)) {
            $min = $this->_config['min_username_length'];
            $max = $this->_config['max_username_length'];
            return $this->error->set_error('AUTH:username_has_illegal_char', array('min' => $min, 'max' => $max));
        }
        //检查用户名是否禁用
        if (!$this->check_username_ban($username)) {
            return $this->error->set_error('AUTH:username_has_ban_char');
        }
        //UC检查用户名
        if ($this->_config['enable_ucenter']) {
            $this->load->library('uc/uc_api');
            $uc_result = $this->uc_api->user_checkname($username);
            if ($uc_result == -1) {
                $min = $this->_config['min_username_length'];
                $max = $this->_config['max_username_length'];
                return $this->error->set_error('AUTH:username_has_illegal_char', array('min' => $min, 'max' => $max));
            } elseif ($uc_result == -2) {
                return $this->error->set_error('AUTH:username_has_ban_char');
            } elseif ($uc_result == -3) {
                return $this->error->set_error('AUTH:username_already_exists');
            }
        }

        return true;
    }

}

/* End of file User_auth_service.php */
/* Location: ./application/service/user/User_auth_service.php */
